<?php
class MemberController extends AdminController {
	public function actionIndex(){
		$this->render('index',array(
			
		));
	}

	public function actionCreate(){
		$model = new Location;
		$this->performAjaxValidation($model);
		if(Yii::app()->request->isPostRequest){
			if(!empty($_POST['Location'])){
				$model->setAttributes($_POST['Location'],false);
			}
			if($model->validate()){
				$result = $model->insert();
				if($result === true){
					Yii::app()->user->setFlash("success","Thêm thành công");
					$this->redirect(array("/admin/location/manage"));
				}
			}
		}

		$this->render('create',array(
			'model' => $model
		));
	}

	public function actionUpdate(){
		$id = @$_GET['id'];
		$model = Location::model()->findByPk(new MongoId($id));
		$this->performAjaxValidation($model);
		if(Yii::app()->request->isPostRequest){
			if(!empty($_POST['Location'])){
				$model->setAttributes($_POST['Location'],false);
			}
			if($model->validate()){
				$result = $model->update();
				if($result === true){
					Yii::app()->user->setFlash("success","Cập nhật thành công");
					$this->redirect(array("/admin/location/manage"));
				}
			}
		}

		$this->render('create',array(
			'model' => $model
		));
	}

	public function actionDelete(){
		$id = @$_GET['id'];
		$model = Location::model()->findByPk(new MongoId($id));
		if(is_object($model)){
			$result = $model->delete();
			if($result === true) echo 1;die;
		}
		echo 0;die;
	}


	public function actionManage(){
		$model= new Member;
		if(Yii::app()->request->isAjaxRequest){
			$model->setAttributes(@$_REQUEST['Member'],false);
		}
		$this->render('manage',array(
			'model' => $model
		));
	}

	protected function performAjaxValidation($model){
	    if(isset($_POST['ajax']) && $_POST['ajax']==='location-form'){
	        echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	}
}